
#include "HTMLParser.h"
#include "URLInputStream.h"
#include "HTMLTokenizer.h"
#include <cstring>
#include <CS240Exception.h>
using namespace std;

HTMLParser::HTMLParser(std::string url) {

   // Initialize the URLInputStream as new, and the tokenizer as new(UIstream)
   UIStream = new URLInputStream("file://" + url);
   tokenizer = new HTMLTokenizer(UIStream);
}

HTMLParser::~HTMLParser() {
   if (UIStream->IsOpen())
      UIStream->Close();
   delete UIStream;
   delete tokenizer;
   UIStream = NULL;
   tokenizer = NULL;
}

void HTMLParser::parse() {
   while(tokenizer->HasNextToken()) {
      HTMLToken token = tokenizer->GetNextToken();
      token = IsBoard(token);

      token = tokenizer->GetNextToken();
      token = IsHistory(token);
   }
}

HTMLToken HTMLParser::IsBoard(HTMLToken & token) {
   if (token.GetValue().compare("board") == 0) {
      while (token.GetType() != TAG_END && token.GetValue().compare("board") != 0) {
         token = tokenizer->GetNextToken();
      }
   }

   return token;
}

HTMLToken HTMLParser::IsHistory(HTMLToken & token) {
   if (token.GetValue().compare("history") == 0) {
      while (token.GetType() != TAG_END && token.GetValue().compare("history") != 0) {
         token = tokenizer->GetNextToken();
      }
   }

   return token;
}

